﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Rotempco.ORM.Generator.ViewModels;
using Rotempco.ORM.Generator.Objects;
using Rotempco.ORM.Generator.Managers;
using System.IO;
using System.ComponentModel;

namespace Rotempco.ORM.Generator
{
    /// <summary>
    /// Interaction logic for TableView.xaml
    /// </summary>
    public partial class TableView : UserControl
    {
        private RotempcoSettings _RotempcoSettings;
        public TableView()
        {
            InitializeComponent();            
        }

        public void SetSettings(RotempcoSettings rotempcoSettings)
        {
            _RotempcoSettings = rotempcoSettings;
            VMTables.Instance.LoadAll(_RotempcoSettings);
        }

        private void button_SetupEntityObjects_Click(object sender, RoutedEventArgs e)
        {
            TableWizard tableWizard = new TableWizard();
            this.Content = tableWizard;
            tableWizard.SetSettings(_RotempcoSettings);
        }

        private void button_GenerateAll_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to auto generate it all ?", "More work ?", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                BackgroundWorker backgroundWorker = new BackgroundWorker();
                backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
                backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
                backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
                backgroundWorker.WorkerReportsProgress = true;
                backgroundWorker.RunWorkerAsync();                
            }
        }

        void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            MessageBox.Show("Done!");
        }

        void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            string tableName = e.UserState as string;

            label_Progress.Content = tableName + " " + VMTables.Instance.SelectedTables.IndexOf(tableName) + " / " + VMTables.Instance.SelectedTables.Count;
        }
                
        void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker backgroundWorker = sender as BackgroundWorker;

            foreach (string tableName in VMTables.Instance.SelectedTables)
            {
                backgroundWorker.ReportProgress(0, tableName);
                this.Dispatcher.Invoke(new Action(
             delegate()
             {
 
                        VMColumns.Instance.LoadAll(_RotempcoSettings, tableName);
                        string aDalImplementation = _RotempcoSettings.GetValue("@ADALType_" + tableName);
                        if (aDalImplementation == "")
                            aDalImplementation = "ADAL";

                        ORMManager.Instance.PerformORM(_RotempcoSettings, new FileInfo(_RotempcoSettings.SettingsFile).Directory.FullName, tableName, tableName, aDalImplementation, VMColumns.Instance.Columns.ToList());
                    }), null);
            }
        }

        private void button_ReloadTablesClick(object sender, RoutedEventArgs e)
        {
            VMTables.Instance.LoadAll(_RotempcoSettings);
        }
    }
}
